package com.designoutput.ui.options
{
	import com.designoutput.elements.BaseElement;
	import com.designoutput.enum.ToolType;
	import com.designoutput.event.ChangeEvent;
	import com.designoutput.event.ColorPadEvent;
	import com.designoutput.event.SelectEvent;
	import com.designoutput.event.SlideControlEvent;
	import com.designoutput.icon.FillIcon;
	import com.designoutput.icon.PencilIcon;
	import com.designoutput.ui.controls.Checkbox;
	import com.designoutput.ui.controls.ColorPad;
	import com.designoutput.ui.controls.Dropdown;
	import com.designoutput.ui.controls.Dropslider;
	import com.designoutput.ui.controls.Literal;
	import com.designoutput.ui.controls.ToolButton;
	import com.designoutput.ui.util.ColorPicker;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import com.designoutput.manager.ResourceManager;

	public class BaseRectOption extends BaseOption
	{
		public var lineGroup:Sprite;
		public var useLineCheckbox:Checkbox;
		public var lineFlag:ToolButton;
		public var lineColorPad:ColorPad;
		public var lineSizeDropdown:Dropdown;
		public var lineAlpha:Dropslider;


		public var fillGroup:Sprite;
		public var useFillChx:Checkbox;
		public var fillFlag:ToolButton;
		public var fillColorPad:ColorPad;
		public var fillAlpha:Dropslider;

		public function BaseRectOption(parent:DesignoutputCard)
		{
			super(parent);

			addChild(depthManager);

			/*************************************/
			lineGroup=new Sprite();
			lineGroup.x=0;

			useLineCheckbox=new Checkbox();
			useLineCheckbox.addEventListener(ChangeEvent.CHANGE, useLineChange);
			useLineCheckbox.x=0;
			useLineCheckbox.y=5;
			useLineCheckbox.check(true);
			lineGroup.addChild(useLineCheckbox);

			lineFlag=new ToolButton(ToolType.None, new PencilIcon(0, 0), 4, 4);
			lineFlag.x=15;
			lineFlag.y=0;
			lineGroup.addChild(lineFlag);

			lineColorPad=new ColorPad(0, true);
			lineColorPad.addEventListener(MouseEvent.MOUSE_DOWN, this.ColorMouseDown);
			lineColorPad.addEventListener(ColorPadEvent.CHOOSE_COLOR, this.lineColorChange);
			lineColorPad.x=42;
			lineColorPad.y=5;
			lineGroup.addChild(lineColorPad);

			lineSizeDropdown=new Dropdown(45, 45);
			lineSizeDropdown.addEventListener(SelectEvent.COMMAND, lineSizeChange);
			lineSizeDropdown.x=75;
			lineSizeDropdown.y=5;

			/*******************/
			var lis:Literal=new Literal(ResourceManager.getValue("rm_line_alpha"));
			lis.x=0;
			lis.y=30;
			lineGroup.addChild(lis);

			lineAlpha=new Dropslider(45, 0, 100, 100, 100);
			lineAlpha.addEventListener(SlideControlEvent.DRAG, this.lineAlphaChange);
			lineAlpha.x=75;
			lineAlpha.y=30;
			lineGroup.addChild(lineAlpha);
			lineGroup.addChild(lineSizeDropdown);

			/*********************************************/
			fillGroup=new Sprite();
			fillGroup.x=0;

			useFillChx=new Checkbox();
			useFillChx.addEventListener(ChangeEvent.CHANGE, useFillChange);
			useFillChx.x=0;
			useFillChx.y=5;
			useFillChx.check(true);
			fillGroup.addChild(useFillChx);

			fillFlag=new ToolButton(ToolType.None, new FillIcon(0, 0), 4, 5);
			fillFlag.x=15;
			fillFlag.y=0;
			fillGroup.addChild(fillFlag);

			fillColorPad=new ColorPad(0xFFFFFF, true);
			fillColorPad.addEventListener(MouseEvent.MOUSE_DOWN, this.ColorMouseDown);
			fillColorPad.addEventListener(ColorPadEvent.CHOOSE_COLOR, this.fillColorChange);
			fillColorPad.x=42;
			fillColorPad.y=5;
			fillGroup.addChild(fillColorPad);


			/*******************/
			var lif:Literal=new Literal(ResourceManager.getValue("rm_fill_alpha"));
			lif.x=0;
			lif.y=30;
			fillGroup.addChild(lif);

			fillAlpha=new Dropslider(45, 0, 100, 100, 100);
			fillAlpha.addEventListener(SlideControlEvent.DRAG, this.fillAlphaChange);
			fillAlpha.x=75;
			fillAlpha.y=30;
			fillGroup.addChild(fillAlpha);

			/*********************************************/

			initLineSize();
			resize();
		}

		private function resize():void
		{
			lineGroup.y = 117;
			fillGroup.y = 167;
		}

		private function initLineSize():void
		{
			var lineSizeArr:Array=new Array(1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 25, 30, 40, 50, 60, 80, 100);
			var len:int=lineSizeArr.length;
			for (var i:int=0; i < len; i++)
			{
				lineSizeDropdown.addItem(lineSizeArr[i].toString(), lineSizeArr[i].toString(), lineSizeArr[i] == "1");
			}
			lineSizeDropdown.bind();
		}

		private function ColorMouseDown(evt:MouseEvent):void
		{
			new ColorPicker(designOutputCard, evt.target as ColorPad).show(true, true);
		}

		private function setDefaultInforTitle():void
		{
			this.elementBaseInfor.setPositionTitle(ResourceManager.getValue("rm_base_position_title"));
			this.elementBaseInfor.setSizeTitle(ResourceManager.getValue("rm_base_size_title"));
		}

		public function update(element:BaseElement,bothShirtAndSleeves:Boolean=false,forSleeveElements:Boolean=false):void
		{
			setDefaultInforTitle();
			super.updateBaseInfor(element,bothShirtAndSleeves,forSleeveElements);
			lineSizeDropdown.Set(element.lineSize.toString());
			lineColorPad.Set(element.lineColor, element.lineAlpha);
			fillColorPad.Set(element.fillColor, element.fillAlpha);
			useFillChx.check(element.useFill);
			useLineCheckbox.check(element.useLine);
			lineAlpha.setValue(element.lineAlpha * 100);
			fillAlpha.setValue(element.fillAlpha * 100);
		}

		private function lineColorChange(evt:ColorPadEvent):void
		{
			var lineColor:uint = this.lineColorPad.value;
			this.designOutputCard.command.changeElementLineColor(lineColor);
		}

		private function fillColorChange(evt:ColorPadEvent):void
		{
			var fillColor:uint = this.fillColorPad.value;
			this.designOutputCard.command.changeElementFillColor(fillColor);
		}

		private function lineSizeChange(evt:SelectEvent):void
		{
			var lineSize:Number=Number(this.lineSizeDropdown.value.toString());
			this.designOutputCard.command.changeElementLineSize(lineSize);
		}

		private function useLineChange(evt:ChangeEvent):void
		{
			this.designOutputCard.command.changeElementUseLine(this.useLineCheckbox.checked);
		}

		private function useFillChange(evt:ChangeEvent):void
		{
			this.designOutputCard.command.changeElementUseFill(this.useFillChx.checked);
		}

		private function lineAlphaChange(evt:SlideControlEvent):void
		{
			var val:Number=new Number(evt.val);
			if (!isNaN(val))
			{
				this.designOutputCard.command.changeElementLineAlpha(val * 0.01);
			}
		}

		private function fillAlphaChange(evt:SlideControlEvent):void
		{
			var val:Number=new Number(evt.val);
			if (!isNaN(val))
			{
				this.designOutputCard.command.changeElementFillAlpha(val * 0.01);
			}
		}

	}
}